package com.cya.cyaproducts;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

import android.util.Log;

public class Crawler {
	private static Crawler instance = null;
	private static int timeoutConnection = 150000;
	private static int timeoutSocket = 100000;
	final String SERVER = "http://gamengon.vn/api/gateway.php";
//	final String SERVER = "http://localhost/cakephp/api/gateway.php?api=SAndroid.getListGame";
	

	protected Crawler() {
		// Exists only to defeat instantiation.
	}

	public static Crawler getInstance() {
		if (instance == null) {
			instance = new Crawler();
		}
		return instance;
	}
	
	public String getListProducts(int currentPage) {
		try {
			String result = "";
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("api", "SAndroid.getListGame"));
			nameValuePairs.add(new BasicNameValuePair("page", Integer.toString(currentPage)));
			InputStream is = null;

			try {
				HttpParams httpParameters = new BasicHttpParams();
				// Set the timeout in milliseconds until a connection is established.
				HttpConnectionParams.setConnectionTimeout(httpParameters, Crawler.timeoutConnection);
				// Set the default socket timeout (SO_TIMEOUT) 
				// in milliseconds which is the timeout for waiting for data.
				HttpConnectionParams.setSoTimeout(httpParameters, Crawler.timeoutSocket);
				
				DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
				HttpPost httppost = new HttpPost(SERVER);
//				httppost.setHeader("Content-Type", "application/json");
//				httppost.setHeader("Accept", "JSON");
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,
						HTTP.ISO_8859_1));
				HttpResponse response = httpclient.execute(httppost);
				HttpEntity entity = response.getEntity();
				is = entity.getContent();
			} catch (Exception e) {
				e.printStackTrace();
				Log.d("log_tag", "Error in http connection " + e.toString());
				return null;
			}

			try {

				ByteArrayOutputStream stream = new ByteArrayOutputStream ();
				byte[] buffer = new byte[8064];
	
				while(true) {
					int rd = is.read(buffer);
					if(rd == -1)break;
					stream.write(buffer, 0 , rd);
				}
	
				stream.flush();
				buffer = stream.toByteArray();
				result = new String(buffer);
				is.close();
				if (!result.contains("id") && !result.contains("desc")) { 
					Log.d("Cya Error", "Data invalid");
					return null;
				}
				return result;
			} catch (Exception e) {
				Log.d("log_tag", "Error converting result " + e.toString());
			}
		} catch (Exception e) {
		}

		return null;
	}
	
	public String getProductById(int id) {
		try {
			String result = "";
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("api", "SAndroid.getGameById"));
			nameValuePairs.add(new BasicNameValuePair("id", Integer.toString(id)));
			InputStream is = null;

			try {
				HttpParams httpParameters = new BasicHttpParams();
				// Set the timeout in milliseconds until a connection is established.
				HttpConnectionParams.setConnectionTimeout(httpParameters, Crawler.timeoutConnection);
				// Set the default socket timeout (SO_TIMEOUT) 
				// in milliseconds which is the timeout for waiting for data.
				HttpConnectionParams.setSoTimeout(httpParameters, Crawler.timeoutSocket);

				DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
				HttpPost httppost = new HttpPost(SERVER);
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,
						HTTP.ISO_8859_1));
				HttpResponse response = httpclient.execute(httppost);
				HttpEntity entity = response.getEntity();
				is = entity.getContent();
			} catch (Exception e) {
				e.printStackTrace();
				Log.d("log_tag", "Error in http connection " + e.toString());
				return null;
			}

			try {

				ByteArrayOutputStream stream = new ByteArrayOutputStream ();
				byte[] buffer = new byte[8064];
	
				while(true) {
					int rd = is.read(buffer);
					if(rd == -1)break;
					stream.write(buffer, 0 , rd);
				}
				
				stream.flush();
				buffer = stream.toByteArray();
				result = new String(buffer);
				is.close();
				if (!result.contains("id") && !result.contains("desc")) {
					Log.d("Cya Error", "Data invalid");
					return null;
				}
				return result;
			} catch (Exception e) {
				Log.d("log_tag", "Error converting result " + e.toString());
			}
		} catch (Exception e) {
		}

		return null;
	}
}
